package core;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JFrame;

/**
 *
 * @author Kuchcik
 */
public class User {
   private int ID;
   private Status AuthorizationID;
   private String login;
   private String password;
   private String FirstName;
   private String LastName;
   private String eMail;
   private boolean isActive;
   private JFrame parent;
   
   /*
   User(int ID) - tworzy Usera po ID, wybierajac go z bazy danych
    */
   public User(int ID,JFrame parent){
        try {
            Statement stmn = SqlConnection.getInstance().getSqlConnection().createStatement();
            stmn.execute("SELECT id_uprawnienia,login,password,imie,nazwisko,email,is_active FROM user WHERE id="+ID+";");
            ResultSet rs = stmn.getResultSet();
            if((rs.next() && rs.getString(1) != null)){
                this.ID=ID;
                this.AuthorizationID=Status.values()[rs.getInt(1)-1];
                this.login=rs.getString(2);
                this.password=rs.getString(3);
                this.FirstName=rs.getString(4);
                this.LastName=rs.getString(5);
                this.eMail=rs.getString(6);
                this.isActive=rs.getBoolean(7);
                        
            }
            else {
                ErrorLogger.getInstance().log("Brak Usera", true, parent);
            }
        } catch (SQLException ex ) {
            ErrorLogger.getInstance().log("Błąd połączenia z bazą danych", true, parent);
        }
   }
   /*
    User(String text, int type) tworzy Usera po wybranym loginie lub mailu itd. 
    * Typy:
    * 0 - login
    * 1 - eMail
    */
   public User(String text,int type, JFrame parent){
       switch(type){
           case 0:{
              try {
                 Statement stmn = SqlConnection.getInstance().getSqlConnection().createStatement();
                 stmn.execute("SELECT id,id_uprawnienia,password,imie,nazwisko,email,is_active FROM user WHERE login='"+text+"';");
                 ResultSet rs = stmn.getResultSet();
                    if((rs.next() && rs.getString(1) != null)){
                        this.login=text;
                        this.ID=rs.getInt(1);
                        this.AuthorizationID=Status.values()[rs.getInt(2)-1];
                        this.password=rs.getString(3);
                        this.FirstName=rs.getString(4);
                        this.LastName=rs.getString(5);
                        this.eMail=rs.getString(6);
                        this.isActive=rs.getBoolean(7);
                        this.parent=parent;
                        }
                       else {
                          ErrorLogger.getInstance().log("Brak Usera", true, parent);
                          }
             } catch (SQLException ex ) {
                    ErrorLogger.getInstance().log("Błąd połączenia z bazą danych", true, parent);
             }    
           break;
           }
           case 1:{
              try {
                 Statement stmn = SqlConnection.getInstance().getSqlConnection().createStatement();
                 stmn.execute("SELECT id,id_uprawnienia,login,password,imie,nazwisko,is_active FROM user WHERE email='"+text+"';");
                 ResultSet rs = stmn.getResultSet();
                    if((rs.next() && rs.getString(1) != null)){
                        this.eMail=text;
                        this.ID=rs.getInt(1);
                        this.AuthorizationID=Status.values()[rs.getInt(2)-1];
                        this.login=rs.getString(3);
                        this.password=rs.getString(4);
                        this.FirstName=rs.getString(5);
                        this.LastName=rs.getString(6);
                        this.isActive=rs.getBoolean(7);
                        this.parent=parent;
                        }
                       else {
                          ErrorLogger.getInstance().log("Brak Usera", true, parent);
                          }
             } catch (SQLException ex ) {
                    ErrorLogger.getInstance().log("Błąd połączenia z bazą danych", true, parent);
             }
           break;
           }
       }
   }
   
   public int getID(){
       return this.ID;
   }

   public int getAuthorizationID(){
       return this.AuthorizationID.status;
   }   

   public Status getAuthorizationStatus(){
       return this.AuthorizationID;
   }
   
   public String getLogin(){
       return this.login;
   }   

   public String getPassword(){
       return this.password;
   }  

   public String getFirstName(){
       return this.FirstName;
   }  

   public String getLastName(){
       return this.LastName;
   }  

   public String getEMail(){
       return this.eMail;
   }  

   public boolean getIsActive(){
       return this.isActive;
   }
   
   public JFrame getJFrameParent(){
       return this.parent;
   }
}
